/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clientereligadora.formularios;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Image;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleInsets;

/**
 *
 * @author leandro.aquino
 */
public final class FormLineChart extends javax.swing.JFrame {

    int estadoChave;
    int estadoMaquina;
    int falhaTensao;
    int falhaCorrente;
    int falhaSobrecarga;
    int avisoSobrecarga;
    int contFalhaTen;
    int contCurtos;
    int contSobrecarga;
    int contAvisSobrecarga;
    int corrCurAtualFA;
    int corrCurAtualFB;
    int corrCurAtualFC;
    int valSobrecargaFA;
    int valSobrecargaFB;
    int valSobrecargaFC;
    int limSobrecarga;
    int temSobrecarga;
    int tenFA;
    int tenFB;
    int tenFC;
    int corrFA;
    int corrFB;
    int corrFC;
    int corrFN;
    int estSwitchServico;
    int estOperacao;
    Date data;
    int linhas;
    DefaultCategoryDataset datasets;
    ImageIcon icon ;
    

    //public FormLineChart(int totalLine, int estadoChave, int estadoMaquina, int falhaTensao, int falhaCorrente, int falhaSobrecarga, int avisoSobrecarga, int contFalhaTen, int contCurtos, int contSobrecarga, int contAvisSobrecarga, int corrCurAtualFA, int corrCurAtualFB, int corrCurAtualFC, int valSobrecargaFA, int valSobrecargaFB, int valSobrecargaFC, int limSobrecarga, int temSobrecarga, int tenFA, int tenFB, int tenFC, int corrFA, int corrFB, int corrFC, int corrFN, int estSwitchServico, int estOperacao, Date data) throws SQLException {
    public FormLineChart(DefaultCategoryDataset ds) throws SQLException {
 icon = new ImageIcon(getClass().getResource("/imagens/enersul.png"));
        this.datasets = ds;
        //        //this.linhas = totalLine;
//        this.estadoChave = estadoChave;
//        this.estadoMaquina = estadoMaquina;
//        this.falhaTensao = falhaTensao;
//        this.falhaCorrente = falhaCorrente;
//        this.falhaSobrecarga = falhaSobrecarga;
//        this.avisoSobrecarga = avisoSobrecarga;
//        this.contFalhaTen = contFalhaTen;
//        this.contCurtos = contCurtos;
//        this.contSobrecarga = contSobrecarga;
//        this.contAvisSobrecarga = contAvisSobrecarga;
//        this.corrCurAtualFA = corrCurAtualFA;
//        this.corrCurAtualFB = corrCurAtualFB;
//        this.corrCurAtualFC = corrCurAtualFC;
//        this.valSobrecargaFA = valSobrecargaFA;
//        this.valSobrecargaFB = valSobrecargaFB;
//        this.valSobrecargaFC = valSobrecargaFC;
//        this.limSobrecarga = limSobrecarga;
//        this.temSobrecarga = temSobrecarga;
//        this.tenFA = tenFA;
//        this.tenFB = tenFB;
//        this.tenFC = tenFC;
//        this.corrFA = corrFA;
//        this.corrFB = corrFB;
//        this.corrFC = corrFC;
//        this.corrFN = corrFN;
//        this.estSwitchServico = estSwitchServico;
//        this.estOperacao = estOperacao;
//        this.data = data;

        initComponents();

        chamaGrafico();

    }

    public void chamaGrafico() throws SQLException {

        try {

            final CategoryDataset dataset = createDataset(datasets);
            JFreeChart chart = createChart(datasets);
            ChartPanel chartPanel = new ChartPanel(chart);
            jPanel1.add(chartPanel, BorderLayout.CENTER);


        } catch (Exception err) {
            System.out.println(err);
        }

    }

    private CategoryDataset createDataset(DefaultCategoryDataset ds1) {



        final DefaultCategoryDataset ds = new DefaultCategoryDataset();



//        for (int i = 0; i <= linhas; i++) {
//
//            ds.addValue(estadoMaquina, "" + i, "Est Maq");
//            ds.addValue(falhaTensao, "" + i, "F.Ten");
//            ds.addValue(falhaCorrente, "" + i, "F.Corr");
//            ds.addValue(falhaSobrecarga, "" + i, "F Sbcgr");
//            ds.addValue(avisoSobrecarga, "" + i, "Av Sbcgr");
//            ds.addValue(contFalhaTen, "" + i, "Con f Ten");
//            ds.addValue(contCurtos, "" + i, "Con Cur");
//            ds.addValue(contSobrecarga, "" + i, "Con Sbcgr");
//            ds.addValue(contAvisSobrecarga, "" + i, "Con AvSbcgr");
//            ds.addValue(corrCurAtualFA, "" + i, "C C FA");
//            ds.addValue(corrCurAtualFB, "" + i, "C C FB");
//            ds.addValue(corrCurAtualFC, "" + i, "C C FC");
//            ds.addValue(valSobrecargaFA, "" + i, "ValSbcgrFA");
//            ds.addValue(valSobrecargaFB, "" + i, "ValSbcgrFB");
//            ds.addValue(valSobrecargaFC, "" + i, "ValSbcgrFC");
//            ds.addValue(limSobrecarga, "" + i, "LimSbcgr");
//            ds.addValue(temSobrecarga, "" + i, "T Sbcgr");
//            ds.addValue(tenFA, "" + i, "tenFA");
//            ds.addValue(tenFB, "" + i, "tenFB");
//            ds.addValue(tenFC, "" + i, "tenFC");
//            ds.addValue(corrFA, "" + i, "corrFA");
//            ds.addValue(corrFB, "" + i, "corrFB");
//            ds.addValue(corrFC, "" + i, "corrFC");
//            ds.addValue(corrFN, "" + i, "corrFN");
//            ds.addValue(estSwitchServico, "" + i, "EstSwSer");
//            ds.addValue(estOperacao, "" + i, "Est Op");
//            System.out.println("estOperacao? " +estOperacao + "linha" + i);
//        }
        return ds;

    }

    private JFreeChart createChart(final CategoryDataset dataset) throws FileNotFoundException, IOException {

        final JFreeChart chart = ChartFactory.createLineChart(
                "Grafico Medições Religadoras", // chart title
                "Parametros", // domain axis label
                "Valores", // range axis label
                dataset, // data
                PlotOrientation.VERTICAL, // orientation
                true, // include legend
                true, // tooltips
                true // urls
                );
 
        chart.setBackgroundPaint(new Color(255, 255, 255, 0));

        //insere imagem de fundo
        chart.setBackgroundImage(icon.getImage());
        
        final CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(new Color(255, 255, 255, 0));
        plot.setBackgroundImageAlpha(0.20f);
                
//salvar grafico png
//       ChartUtilities.writeChartAsPNG(
//               new FileOutputStream(writtenFile),
//                                      chart,
//                                      549, 190,
//                                      null,
//                                      true,    // encodeAlpha
//                                      0 );
        plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
        plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
        plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
        plot.setDomainCrosshairVisible(true);
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        return chart;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel1.setPreferredSize(new java.awt.Dimension(720, 459));
        jPanel1.setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FormLineChart.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormLineChart.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormLineChart.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormLineChart.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    //new FormLineChart(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null).setVisible(true);
                    new FormLineChart(null).setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(FormLineChart.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public static javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
